package nuc.zhangchao.test.service.impl;

import nuc.zhangchao.test.mapper.RoleMapper;
import nuc.zhangchao.test.mapper.UserMapper;
import nuc.zhangchao.test.pojo.User;
import nuc.zhangchao.test.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author Zhang Chao
 * @Date 2020/12/22 14:49
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    //第二步 向sys_user_role 关系表中存储多条数据
    @Override
    public void save(User user, int[] roleIds) {
        //第一步 向sys_user表中存储数据
        int num = userMapper.save(user);
//        int userId=Integer.parseInt(user.getId());
        System.out.println("返回的主键是"+user.getId()+"操作的次数"+num);
        //第二步 向sys_user_role 关系表中存储多条数据
//        userMapper.saveUserRoleRel(user.getId(),roleIds);
        for (int roleId : roleIds) {
//            jdbcTemplate.update("insert into sys_user_role values(?,?)",userId,roleId);
            userMapper.saveUserRoleRel(user.getId(),roleId);
        }
    }

    @Override
    public void del(int userId) {
        userMapper.delUserRoleRel(userId);
        userMapper.del(userId);

    }


}
